package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.JKXueyang;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.dto.JKXueyangDTO;
import com.api.shopapi.service.JKXueyangServie;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.util.DateUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RequestMapping("JKXueyang")
@RestController
public class JKXueyangController extends BaseController{

    @Autowired
    @Qualifier("JKXueyangServieImplOne")
    private JKXueyangServie jkXueyangServie;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    //添加用户血氧检查记录
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKXueyang/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            String xueYang = jsonObject.getString("xueYang");
            int fromType = jsonObject.getInteger("fromType");
            Date inspectTime = jsonObject.getDate("inspectTime");

            JKXueyang jkXueyang=new JKXueyang();
            jkXueyang.setJKXueyangCode(UUID.randomUUID().toString());
            jkXueyang.setUserCode(userCode);
            jkXueyang.setXueYang(xueYang);
            jkXueyang.setFromType(fromType);
            jkXueyang.setInspectTime(inspectTime);
            jkXueyang.setCreateTime(new Date());
            jkXueyangServie.insert(jkXueyang);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKXueyang/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //修改用户血氧检查记录
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKXueyang/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String jKXueyangCode = jsonObject.getString("jKXueyangCode");
            String userCode = jsonObject.getString("userCode");
            String xueYang = jsonObject.getString("xueYang");
            int fromType = jsonObject.getInteger("fromType");
            String inspectTimeStr = jsonObject.getString("InspectTime");
            Date inspectTime=TimeStrToDate(inspectTimeStr);

            JKXueyang jkXueyang = jkXueyangServie.getByJKXueyangCode(jKXueyangCode);
            jkXueyang.setUserCode(userCode);
            jkXueyang.setXueYang(xueYang);
            jkXueyang.setFromType(fromType);
            jkXueyang.setInspectTime(inspectTime);
            jkXueyangServie.update(jkXueyang);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKXueyang/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //删除用户血氧检查记录
    @RequestMapping(value = "Delete",method = RequestMethod.POST)
    public MyResult Delete(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKXueyang/Delete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String jKXueyangCode = jsonObject.getString("jKXueyangCode");

            jkXueyangServie.delete(jKXueyangCode);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKXueyang/Delete", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //用户血氧检查记录列表(app)
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKXueyang/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime = DateUtil.timeAddDay(endTime, 1);
            PageInfo<JKXueyang> xueyangPageInfo = jkXueyangServie.getPageList(pageIndex, pageSize, userCode,startTime,endTime);

            return success(xueyangPageInfo);
        }catch (Exception e){
            insertError("/JKXueyang/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //用户血氧检查记录列表
    @RequestMapping(value = "PageAdm",method = RequestMethod.POST)
    public MyResult PageAdm(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/JKXueyang/PageAdm");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }



            String name = jsonObject.getString("name");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime = DateUtil.timeAddDay(endTime, 1);

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<JKXueyangDTO> xueyangDTOPageInfo = jkXueyangServie.getAdmPageList(pageIndex, pageSize, name, staff.getGovernmentCode(), startTime, endTime);

            return success(xueyangDTOPageInfo);
        }catch (Exception e){
            insertError("/JKXueyang/PageAdm", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
